package tlias.service.Impl;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tlias.mapper.EmpMapper;
import tlias.service.LoginService;
import tlias.pojo.Emp;
import tlias.pojo.LoginResponse;
import tlias.utils.JwtUtils;

import java.util.Map;


@Data
@AllArgsConstructor
@NoArgsConstructor
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private EmpMapper empMapper;
    @Override
    public LoginResponse loginCheck(Emp emp) {
        Emp empSearch = empMapper.searchByUsernameAndPassword(emp);

        if(empSearch != null){
            Map<String,Object> dataMap = Map.of(
                    "id",empSearch.getId(),
                    "username",empSearch.getUsername(),
                    "name",empSearch.getName()
            );
            return new LoginResponse(empSearch.getId(),empSearch.getUsername(),empSearch.getName(), JwtUtils.generateJwt(dataMap));
        }
        else return null;
    }
}
